<?php
/**
 * Created by PhpStorm.
 * User: meng
 * Date: 2021/7/5
 * Time: 5:27 下午
 */
declare (strict_types = 1);
namespace app\admin\controller;
use app\admin\model\AdminUserModel;
use app\admin\validator\AdminUserValidator;
use app\admin\validator\LoginValidator;
use app\utils\JwtUtils;

class Users extends AdminBaseController
{
    public function login () {
        $params = $this->request->param();
        (new LoginValidator())->goCheck($params);
        $data = AdminUserModel::login($params);
        $this->writeJson($data);
    }

    public function refreshToken () {
        $token = $this->request->param('refresh_token');
        $res = (new JwtUtils())->refreshToken($token);
        $this->writeJson($res);
    }

    public function userInfo () {
       $userid = $this->request->userid;
       $user = AdminUserModel::userInfo($userid);
       $this->writeJson($user);
    }

    public function captcha () {
        $captcha = AdminUserModel::createCaptcha();
        $this->writeJson($captcha);
    }

    public function save () {
        $params = $this->request->param();
        (new AdminUserValidator())->goCheck($params, 'create');
        AdminUserModel::createUser($params);
        $this->writeJson();
    }

    public function read ($id) {
        $user = AdminUserModel::findUser($id);
        $this->writeJson($user);
    }

    public function update ($id) {
        $params = $this->request->param();
        (new AdminUserValidator())->goCheck($params, 'update');
        AdminUserModel::updateUser($id, $params);
        $this->writeJson();
    }

    public function delete ($id) {
        AdminUserModel::deleteUser($id);
        $this->writeJson();
    }

    public function userList () {
        $params = $this->request->param();
        $data = AdminUserModel::getUserList($params);
        $this->writeJson($data);
    }

    public function testRedis () {
//        halt(Cache::store('redis')->get('229dad71-00bb-4ada-a6fd-5676c5e0a5b0'));
////        $res = Cache::store('redis')->get('test');
//        $res = Cache::store('redis')->handler()->ttl('test');
//        halt($res);
    }
}
